package alipay.openplatform.edu.campus.jiuye.service.services.user;

import alipay.openplatform.edu.campus.jiuye.common.cache.CacheManagerService;
import alipay.openplatform.edu.campus.jiuye.common.utils.CopyVoPo;
import alipay.openplatform.edu.campus.jiuye.common.utils.ObjectUtils;
import alipay.openplatform.edu.campus.jiuye.dao.mappers.user.UserInfoMapper;
import alipay.openplatform.edu.campus.jiuye.model.po.UserInfoPO;
import alipay.openplatform.edu.campus.jiuye.model.vo.UserBaseInfo;
import alipay.openplatform.edu.campus.jiuye.service.dao.ApiResult;
import alipay.openplatform.edu.campus.jiuye.service.iservice.user.IUserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@Service
public class UserInfoService implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private CacheManagerService cacheManagerService;

    private static final Logger logger = LoggerFactory.getLogger(UserInfoService.class);


    /**
     * 通过userId获取用户信息
     *
     * @param userId
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public UserBaseInfo getUserInfoByUserId(String userId) throws InvocationTargetException, IllegalAccessException {

        UserBaseInfo userBaseInfo = (UserBaseInfo) cacheManagerService.getObject(userId);
        logger.info("尝试从redis中获取的用户信息={}", userBaseInfo);

        UserInfoPO userInfoPO = new UserInfoPO();

        if (ObjectUtils.isNotNull(userBaseInfo)) {
            CopyVoPo.beanUtilsBean.copyProperties(userInfoPO, userBaseInfo);
            logger.info("通过userId获取用户信息|UserInfoService|getUserInfoByUserId|转vo为po,userBaseInfo={},userInfoPO={}", userBaseInfo, userInfoPO);
        }

        if (StringUtils.isBlank(userInfoPO.getUserId())) {
            logger.info("从redis中获取用户信息失败,尝试从数据库中获取用户信息");
            //从数据库中获取用户信息
            userInfoPO = userInfoMapper.selectByPrimaryKey(userId);
            logger.info("尝试从数据库中获取的用户信息={}", userInfoPO);
        }
        if (ObjectUtils.isNull(userInfoPO)) {
            logger.info("从数据库中获取用户信息失败");
            return null;
        }

        userBaseInfo = new UserBaseInfo();//userBaseInfo之前均为null
        CopyVoPo.beanUtilsBean.copyProperties(userBaseInfo, userInfoPO);
        logger.info("通过userId获取用户信息|UserInfoService|getUserInfoByUserId|转po为vo,userBaseInfo={},userInfoPO={}", userBaseInfo, userInfoPO);
        cacheManagerService.setObject(userId, userBaseInfo);
        logger.info("将userBaseInfo打入redis:{}", userBaseInfo);

        return userBaseInfo;
    }

    /**
     * 登录授权后保存用户信息
     *
     * @param userBaseInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveUserInfo(UserBaseInfo userBaseInfo) {
        UserInfoPO userInfoPO = new UserInfoPO();
        try {
            CopyVoPo.beanUtilsBean.copyProperties(userInfoPO, userBaseInfo);
            logger.info("登录授权后保存用户信息|UserInfoService|saveUserInfo|转vo为po,userInfoPO={},userBaseInfo={}", userInfoPO, userBaseInfo);
            return userInfoMapper.insert(userInfoPO);
        } catch (Exception e) {
            logger.error("登录授权后保存用户信息|UserInfoService|saveUserInfo|转vo为po失败或者保存用户信息失败", e.getMessage());
            return 0;
        }
    }

    /**
     * 修改用户信息
     *
     * @param map
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateUserBaseInfo(Map map) {
        return userInfoMapper.updateUserBaseInfo(map);
    }
}